/**
 * 1047. 删除字符串中的所有相邻重复项
 * https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/description/
 */

class Solution {
    public String removeDuplicates(String _s) {
        StringBuffer ret = new StringBuffer();
        char[] s = _s.toCharArray();
        for(char ch : s) {
            if(ret.length() > 0 && ch == ret.charAt(ret.length() - 1)) {
                ret.deleteCharAt(ret.length() - 1);
            } else{
                ret.append(ch);
            }
        }
        return ret.toString();
    }
    // public String removeDuplicates(String s) {
    //     StringBuffer ret = new StringBuffer();
    //     int i = 0;
    //     while(i < s.length()) {
    //         if(ret.length() == 0 || s.charAt(i) != ret.charAt(ret.length() - 1)) {
    //             ret.append(s.charAt(i));
    //         } else {
    //             ret.deleteCharAt(ret.length() - 1);
    //         }
    //         i++;
    //     }
    //     return ret.toString();
    // }
}